package com.ruoyi.model.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.SModel;
import com.ruoyi.model.service.ISModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
 * 源模型Controller
 * 
 * @author cfy
 * @date 2025-02-14
 */




        @RestController
        @RequestMapping("/smodel/smodel")
        public class SModelController extends BaseController {
        @Autowired
        private ISModelService sModelService;

        @GetMapping("/list")
        public TableDataInfo list(SModel sModel) {
        startPage();
        List<SModel> list = sModelService.selectSModelList(sModel);
        return getDataTable(list);
        }

        @Log(title = "源模型", businessType = BusinessType.EXPORT)
        @GetMapping("/export")
        public AjaxResult export(SModel myModel) {
        List<SModel> list = sModelService.selectSModelList(myModel);
        ExcelUtil<SModel> util = new ExcelUtil<>(SModel.class);
        return util.exportExcel(list, "源模型数据");
        }

        @GetMapping(value = "/{id}")
        public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sModelService.selectSModelById(id));
        }

        @Log(title = "源模型", businessType = BusinessType.INSERT)
        @PostMapping
        public AjaxResult add(@RequestParam(value = "file", required = false) MultipartFile file,
        @RequestParam("name") String name,
        @RequestParam("category") String category) {
        SModel sModel = new SModel();
        sModel.setName(name);
        sModel.setCategory(category);
        return toAjax(sModelService.insertSModel(sModel, file));
        }

        @Log(title = "源模型", businessType = BusinessType.UPDATE)
        @PutMapping
        public AjaxResult edit(@RequestParam(value = "file", required = false) MultipartFile file,
        @RequestParam(value = "id", required = false) Long id,
        @RequestParam("name") String name,
        @RequestParam("category") String category) {
        SModel myModel = new SModel();
        myModel.setId(id);
        myModel.setName(name);
        myModel.setCategory(category);
        return toAjax(sModelService.updateSModel(myModel, file));
        }

                @Log(title = "源模型", businessType = BusinessType.DELETE)
                @DeleteMapping("/{ids}")
                public AjaxResult remove(@PathVariable Long[] ids) {
                        // 参数校验
                        if (ids == null || ids.length == 0) {
                                return AjaxResult.error("请选择要删除的数据");
                        }



                        return toAjax(sModelService.deleteSModelByIds( ids));
                }

        }